package name.fuhan.swiperefreshvpagerindicator.view;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import name.fuhan.swiperefreshvpagerindicator.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by Administrator on 2015/12/15.
 */
public class VPagerIndicator extends FrameLayout implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private final LinearLayout view;
    private final LinearLayout mTabs;
    private final TiaoTiaoView mBuoy;
    private final Context context;
    private final ViewPager mVPager;
    private List<CharSequence> tabs;

    class MyPagerAdapater extends PagerAdapter {
        List<MyListViewAdapter> data = new ArrayList<MyListViewAdapter>();
        View[] views;

        @Override
        public int getCount() {
            if (views == null || views.length != data.size()) {
                views = new View[data.size()];
            }
            return data.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return data.get(position).getPageTitle();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            if (views[position] == null) {
                views[position] = View.inflate(getContext(), R.layout.swipe_refresh_list_view, null);
            }
            View view = views[position];
            ViewGroup viewGroup = ((ViewGroup) view.getParent());
            if (viewGroup != null) {
                viewGroup.removeView(view);
            }
            MyListViewAdapter adapter = data.get(position);
            ListView listView = (ListView) view.findViewById(R.id.listView);
            listView.setAdapter(adapter);
            SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefresh);
            swipeRefreshLayout.setOnRefreshListener(adapter);
            adapter.setSwipeRefreshLayout(swipeRefreshLayout);
            container.addView(view);
            adapter.onRefresh();
            return view;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }

    ;

    public VPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        view = ((LinearLayout) View.inflate(context, R.layout.name_fuhan_vpager_indicator, null));
        mTabs = ((LinearLayout) view.findViewById(R.id.tabs));
        mBuoy = (TiaoTiaoView) view.findViewById(R.id.buoy);
        mVPager = ((ViewPager) view.findViewById(R.id.vPager));
        addView(view);
    }

    private void setAdapter(PagerAdapter adapter) {
        int count = adapter.getCount();
        tabs = new ArrayList<CharSequence>(count);
        for (int i = 0; i < count; i++) {
            tabs.add(adapter.getPageTitle(i));
        }
        mTabs.removeAllViews();
        for (int i = 0; i < tabs.size(); i++) {
            CharSequence tab = tabs.get(i);
            TextView textView = new TextView(context);
            textView.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1));
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.BLACK);
            textView.setBackgroundColor(Color.WHITE);
            textView.setPadding(0, 20, 0, 20);
            textView.setText(tab);
            textView.setTag(i);
            textView.setOnClickListener(this);
            mTabs.addView(textView);
        }
        mBuoy.setCount(count);
        mVPager.setAdapter(adapter);
        mVPager.addOnPageChangeListener(this);

    }

    public void setAdapters(MyListViewAdapter... adapters) {
        List<MyListViewAdapter> list = Arrays.asList(adapters);
        MyPagerAdapater myPagerAdapater = new MyPagerAdapater();
        myPagerAdapater.data = list;
        setAdapter(myPagerAdapater);
    }

    public static abstract class MyListViewAdapter<T> extends BaseAdapter implements SwipeRefreshLayout.OnRefreshListener {

    	private Activity activity;

    	/** 需要activity,用来重写notifyDataSetChanged() */
		public MyListViewAdapter(Activity activity) {
    		this.activity = activity;
		}
		
		/** 当前listview所对应的title内容 */
		public abstract String getPageTitle();
		
		/** 当前listview的数据 */
        public abstract ArrayList<T> getData();
        
        /** 刷新时的动作,需要在ui线程中调用sw.setRefreshing(false); */
        public abstract void onRefresh(SwipeRefreshLayout sw);
        

        public void setSwipeRefreshLayout(SwipeRefreshLayout swipeRefreshLayout) {
            this.swipeRefreshLayout = swipeRefreshLayout;
        }

        SwipeRefreshLayout swipeRefreshLayout;

        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public T getItem(int position) {
            return getData().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public void onRefresh() {
            onRefresh(swipeRefreshLayout);
        }

        
        @Override
        public void notifyDataSetChanged() {
        	activity.runOnUiThread(new Runnable() {
				public void run() {
		        	MyListViewAdapter.super.notifyDataSetChanged();
				}
			});
        }
    }


    //    public int getCurrentItem() {
    //        return mVPager.getCurrentItem();
    //    }

    //    ViewPager.OnPageChangeListener pageChangeListener;

    //    public void addOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
    //        pageChangeListener = listener;
    //    }

    @Override
    public void onClick(View v) {
        mVPager.setCurrentItem((Integer) v.getTag());
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mBuoy.setProgress(position + positionOffset);
        //        if (pageChangeListener != null) {
        //            pageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
        //        }
    }

    @Override
    public void onPageSelected(int position) {
        //        if (pageChangeListener != null) {
        //            pageChangeListener.onPageSelected(position);
        //        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //        if (pageChangeListener != null) {
        //            pageChangeListener.onPageScrollStateChanged(state);
        //        }
    }
}

